/**
 * @Desc ：
 * @Author ：刘学松
 * @DateTime ：2021-02-03 16:21
 */
public class Test {
    public static void main(String[] args) {
        int[] arr = {2, 5, 4, 6, 3, 3, 2};
        int midIndex = arr.length >> 1;
        int startIndex = 0;
        int endIndex = arr.length-1;

        int index = findMidIndex(arr, startIndex, endIndex);
        while (index != midIndex) {
            if (midIndex < index) {
                index = findMidIndex(arr, startIndex, index - 1);
            } else {
                index = findMidIndex(arr, index + 1, endIndex);
            }
        }
        System.out.println(arr[index]);
    }

    private static int findMidIndex(int[] arr, int left, int right) {
        int leftIndex = left;
        int rightIndex = right;
        int key = arr[left];

        while (leftIndex < rightIndex) {
            while (arr[rightIndex] >= key && leftIndex < rightIndex) {
                rightIndex--;
            }
            arr[leftIndex] = arr[rightIndex];

            while (arr[leftIndex] <= key && leftIndex < rightIndex) {
                leftIndex++;
            }
            arr[rightIndex] = arr[leftIndex];
        }
        arr[leftIndex] = key;
        return leftIndex;
    }
}
